package com.chenshu.mycnblog.controller;

import com.chenshu.mycnblog.common.MyResponse;
import com.chenshu.mycnblog.common.SessionUtil;
import com.chenshu.mycnblog.model.ArticleInfo;
import com.chenshu.mycnblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description：article控制器层
 * User：chenshu
 * Date：2024-01-02
 * Time：11:31
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist")
     public List<ArticleInfo> getMyArticleList(HttpServletRequest request) {
        List<ArticleInfo> myArticleList;
        int uid = SessionUtil.getLoginUser(request);
        myArticleList = articleService.getMyArticleList(uid);

        return myArticleList;
     }

     @RequestMapping("/detail")
     public Object getDetail(Integer id) {
        if (id != null && id > 0) {
            return articleService.getDetail(id);
        }
        return MyResponse.fail(-1, "查询失败");
     }

    @RequestMapping("/edit")
    public Object editArticle(HttpServletRequest request, ArticleInfo articleInfo) {
        //从session中获取uid
        int uid = SessionUtil.getLoginUser(request);

        int ret;
        if (articleInfo.getId() == 0) {
            //前端没传id的话就是新增文章操作

            //由于新增文章前端没有传uid，因此将登录的用户id传入articleInfo
            articleInfo.setUid(uid);

            //拿到自增id
            ret = articleService.addArticle(articleInfo);
            if (ret >= 1) {
                return MyResponse.success("添加文章成功", ret);
            } else {
                return MyResponse.fail(-100, "添加文章失败");
            }
        } else {
            //传id的话是编辑文章操作

            //更新文章有恶意篡改的风险，需要进行归属人验证
            if (uid != articleInfo.getUid()) {
                return MyResponse.fail(-200, "非法操作：归属人验证失败！");
            }

            ret = articleService.editArticle(articleInfo);
            if (ret == 1) {
                return MyResponse.success("编辑文章成功", ret);
            } else {
                return MyResponse.fail(-100, "编辑文章失败");
            }
        }
    }

    @RequestMapping("/list")
    public List<ArticleInfo> getList (@RequestParam("pindex") Integer pageIndex,
                                      @RequestParam("psize") Integer pageSize) {
        if (pageIndex == null || pageSize == null) {
            return null;
        }
        //计算出偏移量
        Integer offset = (pageIndex - 1) * pageSize;
        return articleService.getArticleListByLimit(offset, pageSize);
    }

    @RequestMapping("/totalpage")
    public Integer getTotalPage(@RequestParam("psize") Integer pageSize) {
        if (pageSize != null) {
            return articleService.getTotalPage(pageSize);
        }
        return null;
    }

    @RequestMapping("/delete")
    public Integer deleteArticle(Integer id) {
       return articleService.deleteArticle(id);
    }
}
